package com.shipwe.loans.utils.dialog

import android.content.Context
import android.graphics.Color
import android.graphics.LinearGradient
import android.graphics.Shader
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.shipwe.loans.R
import com.shipwe.loans.databinding.DialogLuckyDrawGiftBinding
import com.shipwe.loans.pro.ShipweDialog
import com.shipwe.loans.utils.event.AppEventPointHelper
import com.shipwe.loans.utils.event.AppEventPointType

/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/15 15:41
 * @Annotation：获得积分dialog
 */
class LuckyDrawGiftDialog: ShipweDialog {

    private lateinit var binding: DialogLuckyDrawGiftBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    override fun initWindow() {
        val params = window!!.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_scale_anim) //修改动画样式
        window!!.setGravity(Gravity.CENTER)
    }

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String, callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogLuckyDrawGiftBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        val colors = intArrayOf(Color.parseColor("#ffff6629"), Color.parseColor("#ffffa646"))
        val position = floatArrayOf(0f, 1.0f)
        // 创建一个渐变色
        val textShader = LinearGradient(0f, 0f, binding.giftCongratulations.paint.textSize * binding.giftCongratulations.text.length, 0f, colors, position, Shader.TileMode.CLAMP)
        binding.giftCongratulations.paint.setShader(textShader)
        binding.giftCongratulations.invalidate()
    }

    override fun initListener() {
        setOnClickListener(binding.giftGet)
        setOnClickListener(binding.giftClose)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        if(mCallback != null){
            mCallback!!.onClick(v)
        }
    }

    /**
     * 设置埋点备注
     */
    fun setGiftEventRemark(remark: String){
        eventRemark = remark
    }

    /**
     * 设置提示title
     */
    fun setGiftTitleText(title: String){
        binding.giftCongratulations.text = title
    }

    /**
     * 设置获得的积分数
     */
    fun setGiftPointText(pointsText: String){
        binding.giftPoints.text = pointsText
    }

    /**
     * 积分礼物提示
     */
    fun setGiftPointTips(tips: String){
        binding.giftTips.text = tips
    }

    /**
     * 设置按钮文字
     */
    fun setGiftButtonText(btnText: String){
        binding.giftGet.text = btnText
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    override fun onStart() {
        super.onStart()
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_POINTS_SHOW,eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        AppEventPointHelper.appEventPointRequestAction(AppEventPointType.DIALOG.SHIPWE_DIALOG_POINTS_DISMISS,eventRemark)
    }
}